//
//  UIButton+NBtnBadge.m
//  News
//
//  Created by zhangtao on 2018/11/8.
//  Copyright © 2018年 zt.td. All rights reserved.
//

#import "UIButton+NBtnBadge.h"

@implementation UIButton (NBtnBadge)

-(NSInteger)badgeNum{
    return self.badgeNum;
}


-(void)badgeNum:(NSInteger)num andFont:(NSInteger)font{
    
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[UILabel class]] && view.tag == 10000) {
            [view removeFromSuperview];
        }
    }
    
    NSString *numStr = [NSString stringWithFormat:@"%ld",num];
    CGSize titleSize = [numStr boundingRectWithSize:
                        CGSizeMake(1000, 10)
                                         options:NSStringDrawingTruncatesLastVisibleLine |
                        NSStringDrawingUsesLineFragmentOrigin  |
                        NSStringDrawingUsesFontLeading
                                      attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]}
                                         context:nil].size;
    
    CGFloat width = titleSize.width+6;
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width-width/2, -5, width, 12)];
    [lab setTag:10000];
    lab.backgroundColor = [UIColor redColor];
    lab.font = [UIFont systemFontOfSize:font];
    lab.textColor = [UIColor whiteColor];
    lab.layer.cornerRadius = 6;
    lab.layer.masksToBounds = YES;
    lab.textAlignment = NSTextAlignmentCenter;
    lab.text = numStr;
    [self addSubview:lab];
    
}

//-(void)setBadgeNum:(NSInteger)badgeNum{
//    NSString *num = @(badgeNum);
//    self.badgeNum = badgeNum;
//    [self setTitle:num forState:UIControlStateNormal];
//    if (!_badgeLab) {
//        _badgeLab = [UILabel new];
//        _badgeLab.backgroundColor = [UIColor redColor];
//        _badgeLab.layer.cornerRadius = 5;
//        [self addSubview:_badgeLab];
//    }
//    CGSize titleSize = [num boundingRectWithSize:
//                        CGSizeMake(1000, 10)
//                                         options:NSStringDrawingTruncatesLastVisibleLine |
//                        NSStringDrawingUsesLineFragmentOrigin  |
//                        NSStringDrawingUsesFontLeading
//                                      attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10]}
//                                         context:nil].size;
//    
//    CGFloat width = titleSize.width;
////    _badgeLab.frame = CGRectMake(self.frame.size.width-width/2, -5, width, 10);
//    
//    
//    
//}

@end
